package com.card.game.pdk;

import java.util.Vector;

import com.card.common.MessageComm;
import com.card.game.BasePlayer;
import com.card.game.BasePlayer.UserState;
import com.card.game.Card;
import com.card.game.room.RoomState;
import com.card.match.IMatchRoom;
import com.card.match.Match;
import com.card.message.JsonMessageCenter;
import com.card.user.User;
import com.xiaoao.net.common.GameConstants;
import com.xiaoao.net.json.message.JsonMessage;

public class PdkMatchRoom extends PdkRoom implements IMatchRoom{
	private transient Match match;
	private transient boolean allIsEnd;
	
	public PdkMatchRoom(Match match) {
		super("");
		this.match = match;
		this.isMatchRoom = true;
	}
	/**
	 * 跑得快加入玩家
	 * 
	 */
	@Override
	public void addPlayer(User user) {
		PdkPlayer player = new PdkPlayer(user);
		synchronized (this.playerList) {
			if(this.playerList.size()<getMaxUserNum()) {
				player.setTableIndex(this.playerList.size());
				player.getUser().setRoom(this);
				this.playerList.add(player);
				match.initScore(player);
				if(this.playerList.size() == getMaxUserNum()) {
					setState(RoomState.Ready);
				}
			}
		}
		//通知进入
		sendEnterRoom(player);
	}
	
	@Override
	public void doTimedTask() {
		if(state == RoomState.Ready) {
			long waitTime = System.currentTimeMillis() - this.stateChangeTime;
			if(waitTime > 2*GameConstants.SECOND) {
				startGame();
			}
		}else if(state == RoomState.Running) {
			long waitTime = System.currentTimeMillis() - this.curhowsTurnStartTime;
			if(waitTime > 15*GameConstants.SECOND) {
				autoOutCard();
			}
		}else if
		/*(state == RoomState.QiangDizhu) {
			long waitTime = System.currentTimeMillis() - this.curhowsTurnStartTime;
			if(waitTime > 6*GameConstants.SECOND) {
				autoQiangDizhu();
			}
		}else if*/
		(state == RoomState.End && !isEndGame()) {
			long waitTime = System.currentTimeMillis() - this.stateChangeTime;
			if(waitTime > 6*GameConstants.SECOND) {
				startGame();
			}
		}
	} 
	
	@Override
	public boolean isGameEnd() {
		return allIsEnd;
	}

	@Override
	public void clearGameData() {
		super.clearRoom();
	}
// 想client发送进入房间
	@Override
	public void sendEnterRoom(BasePlayer player) {
		JsonMessage message = new JsonMessage(MessageComm.S2C_Match_EnterRoom);
		message.addProperty("roomId", getId());
		JsonMessageCenter.getInstance().sendMessage(message, player);
	}
// 进入比赛房间
	@Override
	public void enterMatchRoom(String userId) {
		PdkPlayer player = getPlayerById(userId);
		if(player == null) {
			return;
		}
		player.setState(UserState.Ready);
		if(allIsState(UserState.Ready) && isFull()) {
			sendRoomInfo();
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
			startGame();
		}else{
			sendRoomInfo();
		}
	}
	
	//开始比赛
	@Override
	public void startGame() {
		sendMatchInfo();
		super.startGame();
	}
    // 断开连接
	@Override
	public void breakConnect(BasePlayer p) {
		super.breakConnect(p);
		sendMatchInfo();
	}
	
	@Override
	public void clearData() {
		for (BasePlayer basePlayer : playerList) {
			match.updateScore(basePlayer);
		}
		super.clearData();
	}
	
	@Override
	public void calAllGame(boolean isDisband) {
		this.allIsEnd = true;
		if(!match.isLastLun()) {
			int curLun = match.getCurLun();
			int runNum = match.getRunTableNum();
			for (BasePlayer player : playerList) {
				JsonMessage message = JsonMessage.createMessage(MessageComm.S2C_Match_RoundEnd);
				message.addProperty("curLun", curLun);
				message.addProperty("rank", match.getRank(player.getUserId()));
				message.addProperty("runNum", runNum);
				message.addProperty("canLeave", match.isCanLeave());
				message.addProperty("allUserNum", match.getUserSize());
				message.addProperty("matchName", match.getMatchSetting().getName());
				JsonMessageCenter.getInstance().sendMessage(message, player);		
			}
		}
		clearRoom();
	}
	
	@Override
	public boolean isMatchRoom() {
		return true;
	}
	
	public boolean isEndGame() {
		return this.playNum >= 2;
	}
	@Override
	public boolean isFull() {
		synchronized (playerList) {
			return this.playerList.size() == getMaxUserNum();
		}
	}
	@Override
	public void autoOutCard() {
		PdkPlayer curPlayer = getPlayerByTableIndex(this.curwhosTurn);
		if(curPlayer != null) {
			Vector<Card> hand = new Vector<>();
			if(this.lastOutHand == null) {
				Card outCard = curPlayer.cards.get(curPlayer.cards.size()-1);
				hand.add(outCard);
			}
			handOut(curPlayer, hand);
		}
	}
	
	@Override
	public void allNotQiang() {
		changeWhoTurn(getNextPlayer());
		PdkPlayer player = getPlayerByTableIndex(this.curwhosTurn);
		setState(RoomState.Running);
		this.diZhuId = player.getUserId();
//		broadDiPai();
//		player.addDiPai(pdkRoomInfo.getDiPai());
		sendUpdateCards(player);
		broadChuPai();
	}
	
/*	public void autoQiangDizhu() {
		PdkPlayer curPlayer = getPlayerByTableIndex(this.curwhosTurn);
		if(curPlayer != null) {
			qiangDizhu(curPlayer, false);
		}
	}*/
	//发送比赛信息
	@Override
	public void sendMatchInfo() {
		int curLun = match.getCurLun();
		int runNum = match.getRunTableNum();
		for (BasePlayer player : playerList) {
			JsonMessage message = new JsonMessage(MessageComm.S2C_Match_Info);
			message.addProperty("userId", player.getUserId());
			message.addProperty("rank", match.getRank(player.getUserId()));
			message.addProperty("runNum", runNum);
			message.addProperty("curLun", curLun);
			message.addProperty("allUserNum", match.getUserSize());
			message.addProperty("matchName", match.getMatchSetting().getName());
			JsonMessageCenter.getInstance().sendMessage(message, player);
		}
	}
}
